package com.smart.builder.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import com.smart.persistence.entity.Destination;
import com.smart.persistence.entity.Hotel;
import com.smart.persistence.entity.Image;
import com.smart.persistence.service.SmartBasicCrudDAO;
import com.smart.service.infohotel.InfoWebHotelService;
import com.smart.service.search.SearchWebHotelService;
import com.smart.service.util.Logging;

public class BuilderEntityHotel {

	private static final Logging LOGGER = Logging.getLogger(BuilderEntityHotel.class
			.getName());

	private Hotel hotel = null;
	private int codeHotel;

	public BuilderEntityHotel(int codeH, EntityManager em) {
		super();
		this.codeHotel = codeH;
		find(em);
		if (hotel == null) {
			hotel = new Hotel();
			hotel.setCodHotel(codeH);
		}
	}

	public BuilderEntityHotel save(EntityManager em) {
		LOGGER.debug("save " + hotel);
		if (hotel.getId() != 0) {
			em.merge(hotel);
		} else {
			em.persist(hotel);
		}
		em.flush();
		find(em);
		return this;
	}

	public BuilderEntityHotel find(EntityManager em) {
		LOGGER.debug("find");
		hotel = SmartBasicCrudDAO.getInstance().consultaQuerySingle(em,
				Hotel.class, Hotel.findByCodeHotel, codeHotel);
		return this;
	}

	public BuilderEntityHotel setListMapRoom(List<Map<String, String>> listMapRoom) {
		return this;
	}

	public BuilderEntityHotel setListImages(List<String> listImages) {
		LOGGER.debug("setListImages");
		if (hotel.getListImage() == null) {
			hotel.setListImage(new ArrayList<Image>());
		}
		for (String string : listImages) {
			hotel.getListImage().add(new Image(string, hotel));
		}
		return this;
	}

	public BuilderEntityHotel setDestination(Destination entityDest) {
		LOGGER.debug("setDestination");
		assert hotel != null;
		if (hotel.getDestinations() == null) {
			hotel.setDestinations(new ArrayList<Destination>());
		}
		hotel.getDestinations().add(entityDest);
		return this;
	}

	public BuilderEntityHotel setMap(Map<String, String> map) {
		addInfoName(map);
		addInfoLinkImage(map);
		addInfoQuality(map);
		addInfoLinkSelection(map);
		addInfoZipCode(map);
		addInfoCity(map);
		addInfoAddress(map);
		addInfoLatitude(map);
		addInfoLongitude(map);
		addInfoLastUpdate();
		return this;
	}

	private void addInfoLastUpdate() {
		hotel.setLastUpdate(new Date());
	}

	private void addInfoLongitude(Map<String, String> map) {
		if (map.containsKey(InfoWebHotelService._hotelLongitude))
			hotel.setLongitude(map.get(InfoWebHotelService._hotelLongitude));
	}

	private void addInfoLatitude(Map<String, String> map) {
		if (map.containsKey(InfoWebHotelService._hotelLatitude))
			hotel.setLatitude(map.get(InfoWebHotelService._hotelLatitude));
	}

	private void addInfoAddress(Map<String, String> map) {
		if (map.containsKey(InfoWebHotelService._hotelAddress1))
			hotel.setEndereco(map.get(InfoWebHotelService._hotelAddress1));
		if (map.containsKey(InfoWebHotelService._hotelAddress2))
			hotel.setEndereco(hotel.getEndereco() + map.get(InfoWebHotelService._hotelAddress2));

	}

	private void addInfoCity(Map<String, String> map) {
		if (map.containsKey(InfoWebHotelService._hotelCity))
			hotel.setCidade(map.get(InfoWebHotelService._hotelCity));
	}

	private void addInfoZipCode(Map<String, String> map) {
		if (map.containsKey(InfoWebHotelService._hotelPostCode))
			hotel.setCep(map.get(InfoWebHotelService._hotelPostCode));
	}

	private void addInfoLinkSelection(Map<String, String> map) {
		if (map.containsKey(SearchWebHotelService._linkSelection))
			hotel.setLinkSelection(map
					.get(SearchWebHotelService._linkSelection));
	}

	private void addInfoQuality(Map<String, String> map) {
		if (map.containsKey(SearchWebHotelService._quality))
			hotel.setQuality(map.get(SearchWebHotelService._quality));
		if (map.containsKey(InfoWebHotelService._hotelStarRating))
			hotel.setQuality(map.get(InfoWebHotelService._hotelStarRating));

	}

	private void addInfoLinkImage(Map<String, String> map) {
		if (map.containsKey(SearchWebHotelService._linkImage))
			hotel.setLinkImage(map.get(SearchWebHotelService._linkImage));
	}

	private void addInfoName(Map<String, String> map) {
		if (map.containsKey(SearchWebHotelService._name))
			hotel.setName(map.get(SearchWebHotelService._name));
	}

	public Hotel getHotel() {
		return hotel;
	}

}
